package com.example.gfsquadwebviewproject.fragment.setting;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.gfsquadwebviewproject.R;
import com.example.gfsquadwebviewproject.data.ConfigData;
import com.example.gfsquadwebviewproject.data.Constant;

public class ChangePasswordFragment extends Fragment {

    String TAG = "ChangePassword";

    EditText et_former_password;
    EditText et_new_password;
    EditText et_new_again_password;

    TextView tv_password_err;

    Button bt_save_password;

    Constant constant;
    ConfigData configData;
    String former_password;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configData = new ConfigData(getContext());
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.setting_change_password,container,false);

        et_former_password = view.findViewById(R.id.et_former_password);
        et_new_password = view.findViewById(R.id.et_new_password);
        et_new_again_password = view.findViewById(R.id.et_new_again_password);

        tv_password_err = view.findViewById(R.id.tv_password_err);

        bt_save_password = view.findViewById(R.id.bt_save_password);


        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        bt_save_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                former_password = configData.getPassword();
                String newPassword = et_new_password.getText().toString();
                String newAgainPassword = et_new_again_password.getText().toString();

                if (former_password.equals(et_former_password.getText().toString())){
                    if (newPassword.length()==6){
                        if (newPassword.equals(newAgainPassword)){
                            configData.savePassword(newPassword);
                            cleanView();
                            Toast.makeText(getContext(),"密码修改完成",Toast.LENGTH_SHORT).show();
                        }
                        else {
                            showtPasswordErr("两次填写密码不一致");
                        }
                    }
                    else {
                        showtPasswordErr("请输入六位数字的新密码");
                    }
                }
                else {
                    showtPasswordErr("原密码不正确");
                }
            }
        });

    }

    private void showtPasswordErr(String msg){
        tv_password_err.setVisibility(View.VISIBLE);
        tv_password_err.setText(msg);
    }

    public void cleanView(){
        et_former_password.setText("");
        et_new_password.setText("");
        et_new_again_password.setText("");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy");
    }
}
